package net.fornwall.eclipsecoder.perlsupport;

import java.io.ByteArrayInputStream;

import net.fornwall.eclipsecoder.languages.LanguageSupport;
import net.fornwall.eclipsecoder.stats.CodeGenerator;
import net.fornwall.eclipsecoder.stats.ProblemStatement;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.epic.core.Constants;
import org.epic.core.util.NatureUtilities;

public class PerlLanguageSupport extends LanguageSupport {

    @Override
    protected CodeGenerator createCodeGenerator(ProblemStatement problemStatement) {
        return new PerlCodeGenerator(problemStatement);
    }

    @Override
    protected IFile createLanguageProject(IProject project) throws Exception {
        NatureUtilities.addNature(project, Constants.PERL_NATURE_ID);

        IFile testsFile = project.getFile(getProblemStatement().getSolutionClassName() + "Test.pl");
        testsFile.create(new ByteArrayInputStream(getCodeGenerator().getTestsSource().getBytes()),
                true, null);

        IFile sourceFile = project.getFile(getSolutionFileName());
        sourceFile.create(new ByteArrayInputStream(getInitialSource().getBytes()), true, null);

        return sourceFile;
    }

    @Override
    protected String getCodeEditorID() {
        return Constants.PERL_EDITOR_ID;
    }

    @Override
    protected String getCodeTemplate() {
        return "package $CLASSNAME$;\nuse strict;\n\n" + "sub new {\n" + "    my ($class) = @_;\n"
                + "    my $self = {};\n" + "    bless $self, $class;\n" + "    return $self;\n"
                + "}\n\n" + "sub $METHODNAME$($PERL_SIGNATURE$) {\n"
                + "    my ($self, $METHODPARAMS$) = @_;\n" + "    return $DUMMYRETURN$;\n"
                + "}\n\nreturn 1;\n";
    }

    @Override
    public String getLanguageName() {
        return "Perl";
    }

    @Override
    public String getPerspectiveID() {
        return "org.epic.core.Perspective";
    }

    @Override
    protected String getSolutionFileName() {
        return getProblemStatement().getSolutionClassName() + ".pm";
    }

    @Override
    protected String getSubmission() throws Exception {
        return null;
    }

}
